//
//  CategoryViewController.m
//  Malena
//
//  Created by Dawn on 12-11-4.
//  Copyright (c) 2012年 Dawn. All rights reserved.
//

#import "CategoryViewController.h"

@interface CategoryViewController ()

@end

@implementation CategoryViewController
@synthesize categoryData = _categoryData;
@synthesize delegate;


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    button.frame = CGRectMake(0, 0, 45, 45);
    [self.view addSubview:button];
    
    self.view.backgroundColor  = [UIColor whiteColor];
    
    if (self.interfaceOrientation == UIDeviceOrientationPortrait || self.interfaceOrientation == UIDeviceOrientationPortraitUpsideDown){
        categoryList = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, SCREEN_HEIGHT - 50) style:UITableViewStylePlain];
    }else{
        categoryList = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_HEIGHT, SCREEN_WIDTH - 50) style:UITableViewStylePlain];
    }
    categoryList.delegate = self;
    categoryList.dataSource = self;
    categoryList.rowHeight = 40;
    categoryList.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    [self.view addSubview:categoryList];
    
}

- (void)backButtonClick:(id)sender{
    if (IOSVersion_5) {
        [self.presentingViewController willRotateToInterfaceOrientation:self.presentingViewController.interfaceOrientation duration:0];
        [self.presentingViewController dismissViewControllerAnimated:YES completion:^{}];
    }else{
        [self.parentViewController dismissModalViewControllerAnimated:YES];
    }

}


- (void) setCategoryData:(NSArray *)categoryData{
    _categoryData = categoryData;
    if (categoryList) {
        [categoryList reloadData];
    }
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([delegate respondsToSelector:@selector(didSelectedCateforyAtIndex:)]) {
        [delegate didSelectedCateforyAtIndex:indexPath.row];
        [self dismissModalViewControllerAnimated:YES];
    }
}

#pragma mark -
#pragma mark UITableViewDatesource
- (NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _categoryData.count;
}

- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"Category"];
    if (!cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Category"] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
        cell.textLabel.font = [UIFont systemFontOfSize:14.0f];
        cell.textLabel.textColor = [UIColor colorWithRed:180.0/255.0 green:185.0/255.0f blue:193.0/255.0 alpha:1.0f];
    }
    cell.textLabel.text = [[self.categoryData objectAtIndex:indexPath.row] objectForKey:@"Name"];
    return cell;
}



#pragma mark -
#pragma mark UIViewControllerRotation
// 支持旋转
- (BOOL)shouldAutorotate{
    return YES;
}

// 支持所有的方向
- (NSUInteger)supportedInterfaceOrientations{
    return UIInterfaceOrientationMaskAll;
}

-(BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation{
	return YES;
}

- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{
    switch (toInterfaceOrientation) {
        case UIInterfaceOrientationLandscapeLeft:
        case UIInterfaceOrientationLandscapeRight:{
            categoryList.frame = CGRectMake(0, 50, SCREEN_HEIGHT, SCREEN_WIDTH - 50);
            break;
        }
        case UIInterfaceOrientationPortrait:
        case UIInterfaceOrientationPortraitUpsideDown:{
            categoryList.frame = CGRectMake(0, 50, SCREEN_WIDTH,SCREEN_HEIGHT - 50);
            break;
        }
        default:
            break;
    }
}

@end
